﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ComponentGIS.Class;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.SystemUI;

namespace ComponentGIS.View
{
    public partial class FormQueryByAttribute : Form
    {
        #region 字段属性
        IMap pMap;
        IMapControl2 pMapControl;
        #endregion

        #region 构造器
        public FormQueryByAttribute(IMapControl2 pMapControl)
        {
            InitializeComponent();
            pMap = pMapControl.Map;
            this.pMapControl = pMapControl;
            comboBoxLayerName.DataSource = BasicClass.GetFeatureLayerNameList(pMap);
            comboBoxLayerName.SelectedIndex = 0;
            comboBoxSelectMethod.SelectedIndex = 0;
        }
        #endregion

        #region 事件
        private void comboBoxLayerName_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                textBoxWhere.Clear();
                IFeatureLayer pFeaLyr = (IFeatureLayer)pMap.get_Layer(comboBoxLayerName.SelectedIndex);
                listBoxFields.DataSource = BasicClass.GetFieldNameList(pFeaLyr.FeatureClass);
                labelwhere.Text = pFeaLyr.Name + " WHERE";
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void listBoxFields_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                string sFieldName = listBoxFields.SelectedItem.ToString();
                IFeatureLayer pFeaLyr = (IFeatureLayer)pMap.get_Layer(comboBoxLayerName.SelectedIndex);
                listBoxValues.DataSource = BasicClass.GetFieldValueList(pFeaLyr.FeatureClass, sFieldName);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void buttonEqual_Click(object sender, EventArgs e)
        {
            InsertString(buttonEqual.Text);
        }

        private void buttonNotEqual_Click(object sender, EventArgs e)
        {
            InsertString(buttonNotEqual.Text);
        }

        private void buttonLike_Click(object sender, EventArgs e)
        {
            InsertString(buttonLike.Text);
        }

        private void buttonGreater_Click(object sender, EventArgs e)
        {
            InsertString(buttonGreater.Text);
        }

        private void buttonGeaterEqual_Click(object sender, EventArgs e)
        {
            InsertString(buttonGeaterEqual.Text);
        }

        private void buttonAnd_Click(object sender, EventArgs e)
        {
            InsertString(buttonAnd.Text);
        }

        private void buttonLess_Click(object sender, EventArgs e)
        {
            InsertString(buttonLess.Text);
        }

        private void buttonLessEqual_Click(object sender, EventArgs e)
        {
            InsertString(buttonLessEqual.Text);
        }

        private void buttonOr_Click(object sender, EventArgs e)
        {
            InsertString(buttonOr.Text);
        }

        private void buttonUnderLine_Click(object sender, EventArgs e)
        {
            string str = buttonUnderLine.Text;
            int pointerIndex = textBoxWhere.SelectionStart;
            textBoxWhere.Text = textBoxWhere.Text.Insert(pointerIndex, str);
            textBoxWhere.SelectionStart = pointerIndex + str.Length;
            textBoxWhere.Focus();
        }

        private void buttonPercent_Click(object sender, EventArgs e)
        {
            InsertString(buttonPercent.Text);
        }

        private void buttonBrackets_Click(object sender, EventArgs e)
        {
            string str = buttonBrackets.Text;
            int pointerIndex = textBoxWhere.SelectionStart;
            textBoxWhere.Text = textBoxWhere.Text.Insert(pointerIndex, str);
            textBoxWhere.SelectionStart = pointerIndex + 1;
            textBoxWhere.Focus();
        }

        private void buttonNot_Click(object sender, EventArgs e)
        {
            InsertString(buttonNot.Text);
        }

        private void buttonIs_Click(object sender, EventArgs e)
        {
            InsertString(buttonIs.Text);
        }

        private void buttonQuotes_Click(object sender, EventArgs e)
        {
            string str = buttonQuotes.Text;
            int pointerIndex = textBoxWhere.SelectionStart;
            textBoxWhere.Text = textBoxWhere.Text.Insert(pointerIndex, str);
            textBoxWhere.SelectionStart = pointerIndex + 1;
            textBoxWhere.Focus();
        }

        private void listBoxFields_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            InsertString(listBoxFields.SelectedItem.ToString());
        }

        private void listBoxValues_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            InsertString(listBoxValues.SelectedItem.ToString());
        }

        private void buttonClear_Click(object sender, EventArgs e)
        {
            textBoxWhere.Clear();
        }

        private void buttonApply_Click(object sender, EventArgs e)
        {
            SelectFeaturesByAttribute();
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            SelectFeaturesByAttribute();
            this.Close();
        }

        private void buttonClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void checkBoxSelectableLayers_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBoxSelectableLayers.Checked == true)
                comboBoxLayerName.DataSource =
                    BasicClass.GetSelectableLayerNameList(pMap);
            else
                comboBoxLayerName.DataSource =
                    BasicClass.GetFeatureLayerNameList(pMap);
        }
        #endregion

        #region 方法
        private void SelectFeaturesByAttribute()
        {
            //获取参数
            IFeatureLayer pFeaLyr = (IFeatureLayer)pMap.get_Layer(comboBoxLayerName.SelectedIndex);
            esriSelectionResultEnum selectMethod;
            switch (comboBoxSelectMethod.SelectedItem.ToString())
            {
                case "创建新选择集":
                    pMap.ClearSelection();
                    selectMethod = esriSelectionResultEnum.esriSelectionResultNew;
                    break;
                case "添加到当前选择集":
                    selectMethod = esriSelectionResultEnum.esriSelectionResultAdd;
                    break;
                case "从当前选择集中删除":
                    selectMethod = esriSelectionResultEnum.esriSelectionResultXOR;
                    break;
                case "从当前选择集中选择":
                    selectMethod = esriSelectionResultEnum.esriSelectionResultAnd;
                    break;
                default:
                    pMap.ClearSelection();
                    selectMethod = esriSelectionResultEnum.esriSelectionResultNew;
                    break;
            }
            string sWhere = textBoxWhere.Text;
            //执行查询
            bool OK = BasicClass.SelectFeatures(pFeaLyr, selectMethod, sWhere, pMap as IActiveView);
            if (!OK)
            {
                MessageBox.Show("查找失败！");
                return;
            }
            //定位选择集
            ICommand cmd = new ControlsZoomToSelectedCommandClass();
            cmd.OnCreate(pMapControl);
            cmd.OnClick();
        }

        private void InsertString(string str)
        {
            int pointerIndex = textBoxWhere.SelectionStart;
            textBoxWhere.Text = textBoxWhere.Text.Insert(pointerIndex, str);
            textBoxWhere.SelectionStart = pointerIndex + str.Length;
            textBoxWhere.Focus();
        }
        #endregion

    }
}
